/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 *  The greatest achievement is selflessness. The greatest worth is self-mastery.
 *  The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 *  The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 *  The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 *  The greatest goodness is a peaceful mind. The greatest patience is humility.
 *  The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 *  The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 * 
 * @author Ashin Wimalajeewa (ash)
 */
package org.javaongems.gwk.client;

import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;

public class Card extends Panel {
	final static public String STYLE_LEFT_HDR = "gwk-Card-lhdr";
	final static public String STYLE_SPLIT_LEFT_HDR = "gwk-Card-slhdr";
  
	private Element header, leftHeader, titleContainer, body, bodyContainer, footer;
	private Widget titleWidget, bodyWidget;
	private CardDeck cardDeck;
	private boolean openned = false;
	private ArrayList children = new ArrayList();
	private FocusableElemWidget focusableTitle;
	
	public Card() {
		Element elm = DOM.createDiv();
		setElement(elm);
		setStyleName("gwk-Card");
		buildFrame();
	}
	
	public Widget getTitleWidget() {
		return titleWidget;
	}
	
	public Widget getBodyWidget() {
		return bodyWidget;
	}
	
	public void setTitleWidget(Widget title) {
		if (title == titleWidget)
			return;
		if (titleWidget != null) {
			children.remove(titleWidget);
			disown(titleWidget);
			titleWidget = null;
		}
		if (title != null) {
			titleWidget = title;
			children.add(titleWidget);
			adopt(titleWidget, titleContainer);
		}
	}
	
	public void setBodyWidget(Widget body) {
		if (body == bodyWidget)
			return;
		if (bodyWidget != null) {
			children.remove(bodyWidget);
			disown(bodyWidget);
			bodyWidget = null; 
		}
		if (body != null) {
			bodyWidget = body;
			children.add(bodyWidget);
			adopt(bodyWidget, bodyContainer);
		}
	}
	
	public void refresh() {
		boolean asFirstCard = cardDeck.isFirstCard(this);
		if (!asFirstCard) {
			Card prev = cardDeck.getPreviousCard(this);
			asFirstCard = prev.isOpenned();
		}
		UIObject.setStyleName(leftHeader, asFirstCard ? STYLE_SPLIT_LEFT_HDR: STYLE_LEFT_HDR, false);
		UIObject.setStyleName(leftHeader, asFirstCard ? STYLE_LEFT_HDR: STYLE_SPLIT_LEFT_HDR, true);
		boolean asLastCard = openned;
		if (!asLastCard)
			asLastCard = cardDeck.isLastCard(this);
		UIObject.setVisible(footer, asLastCard);
	}
	
	protected void internalOpen() {
	}
	
	public void open() {
		if (openned)
			return;
		if (!cardDeck.onBeforeCardOpenned(this))
			return;
		
		UIObject.setVisible(body, true);
		UIObject.setVisible(footer, true);
		openned = true;
		internalOpen();
		cardDeck.onCardOpenned(this);
		refreshNextCard();
	}

	protected void internalClose() {
	}
	
	public void close() {
		if (!openned)
			return;
		if (!cardDeck.onBeforeCardClosed(this))
			return;

		openned = false;
		internalClose();
		UIObject.setVisible(body, false);
		refresh();
		cardDeck.onCardClosed(this);
		refreshNextCard();
	}
	
	private void refreshNextCard() {
		Card next = cardDeck.getNextCard(this);
		if (next != null)
			next.refresh();
	}
	
	public void onBrowserEvent(Event event) {
		int type = DOM.eventGetType(event);
		if (type == Event.ONCLICK) {
			Element targ = DOM.eventGetTarget(event);
			if (DOM.isOrHasChild(titleContainer, targ)) {
				DOM.eventCancelBubble(event, true);
				toggleCardDisplay();
			}
		} else if (type == Event.ONKEYUP) {
			if (DOM.eventGetKeyCode(event) == (char)32) {
				DOM.eventCancelBubble(event, true);
				toggleCardDisplay();
			}
		}
	}

	private void toggleCardDisplay() {
		if (openned)
			close();
		else
			open();
	}
	
	/*pkg*/ void setCardDeck(CardDeck deck) {
		cardDeck = deck;
	}
	
	private void buildFrame() {
		Element tbody = buildHeader();
		buildTitle(tbody);
		buildBody();
		buildFooter();
	}

	private Element buildHeader() {
		header = DOM.createTable();
		DOM.appendChild(getElement(), header);
		DOM.setAttribute(header, "className", "gwk-Card-header");
		Element tbody = DOM.createTBody();
		DOM.appendChild(header, tbody);
		Element tr = DOM.createTR();
		DOM.appendChild(tbody, tr);
		
		leftHeader = DOM.createTD();
		DOM.appendChild(tr, leftHeader);
		DOM.setAttribute(leftHeader, "className", "gwk-Card-lhdr");
		DOM.setAttribute(leftHeader, "rowSpan", "2");
		
		Element centre = DOM.createTD();
		DOM.appendChild(tr, centre);
		DOM.setAttribute(centre, "className", "gwk-Card-chdr");
		
		Element right = DOM.createTD();
		DOM.appendChild(tr, right);
		DOM.setAttribute(right, "className", "gwk-Card-rhdr");
		 DOM.setAttribute(right, "rowSpan", "2");
		
		return tbody;
	}
	
	private void buildTitle(Element headerTbody) {
		Element tr = DOM.createTR();
		DOM.appendChild(headerTbody, tr);
		
		titleContainer = DOM.createTD();
		focusableTitle = new FocusableElemWidget(this, titleContainer);
		adopt(focusableTitle, tr);
	}
	
	private void buildBody() {
		body = DOM.createTable();
		DOM.appendChild(getElement(), body);
		DOM.setAttribute(body, "className", "gwk-Card-body");
		UIObject.setVisible(body, false);
		Element tbody = DOM.createTBody();
		DOM.appendChild(body, tbody);
		Element tr = DOM.createTR();
		DOM.appendChild(tbody, tr);
		
		Element left = DOM.createTD();
		DOM.appendChild(tr, left);
		DOM.setAttribute(left, "className", "gwk-Card-lbdy");
		
		bodyContainer = DOM.createTD();
		DOM.appendChild(tr, bodyContainer);
		DOM.setAttribute(bodyContainer, "className", "gwk-Card-cbdy");
		
		Element right = DOM.createTD();
		DOM.appendChild(tr, right);
		DOM.setAttribute(right, "className", "gwk-Card-rbdy");
	}
	
	private void buildFooter() {
		footer = DOM.createTable();
		DOM.appendChild(getElement(), footer);
		DOM.setAttribute(footer, "className", "gwk-Card-footer");
		UIObject.setVisible(footer, false);
		Element tbody = DOM.createTBody();
		DOM.appendChild(footer, tbody);
		Element tr = DOM.createTR();
		DOM.appendChild(tbody, tr);
		
		Element left = DOM.createTD();
		DOM.appendChild(tr, left);
		DOM.setAttribute(left, "className", "gwk-Card-lftr");
		DOM.setInnerHTML(left, "&nbsp;");
		
		Element centre = DOM.createTD();
		DOM.appendChild(tr, centre);
		DOM.setAttribute(centre, "className", "gwk-Card-cftr");
		
		Element right = DOM.createTD();
		DOM.appendChild(tr, right);
		DOM.setAttribute(right, "className", "gwk-Card-rftr");
		DOM.setInnerHTML(right, "&nbsp;");
	}

	public Iterator iterator() {
		return children.iterator();
	}

	public boolean remove(Widget w) {
		if (w == null)
			return false;
		if (w == bodyWidget) {
			setBodyWidget(null);
			return true;
		} else if (w == titleWidget) {
			setTitleWidget(null);
			return true;
		}
		return false;
	}
	
	public boolean isOpenned() {
		return openned;
	}
}
